package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.business.domain.CcHkInfo;
import com.lxmes.business.domain.CcOrderInfo;
import com.lxmes.business.mapper.CcHkInfoMapper;
import com.lxmes.business.mapper.CcOrderInfoMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcHkMainMapper;
import com.lxmes.business.domain.CcHkMain;
import com.lxmes.business.service.ICcHkMainService;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.rowset.serial.SerialException;

/**
 * 回款主表Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-08-27
 */
@Service
public class CcHkMainServiceImpl implements ICcHkMainService 
{
    @Autowired
    private CcHkMainMapper ccHkMainMapper;

    @Autowired
    private CcHkInfoMapper ccHkInfoMapper;

    /**
     * 查询回款主表
     * 
     * @param id 回款主表主键
     * @return 回款主表
     */
    @Override
    public CcHkMain selectCcHkMainById(Long id)
    {
        return ccHkMainMapper.selectCcHkMainById(id);
    }

    /**
     * 查询回款主表列表
     * 
     * @param ccHkMain 回款主表
     * @return 回款主表
     */
    @Override
    public List<CcHkMain> selectCcHkMainList(CcHkMain ccHkMain)
    {
        return ccHkMainMapper.selectCcHkMainList(ccHkMain);
    }

    /**
     * 新增回款主表
     * 
     * @param ccHkMain 回款主表
     * @return 结果
     */
    @Override
    public int insertCcHkMain(CcHkMain ccHkMain)
    {
        //如果客户和订单存在了则提示不能新增

        CcHkMain searchHkMain = new CcHkMain();
        searchHkMain.setCusId(ccHkMain.getCusId());
        searchHkMain.setOrderId(ccHkMain.getOrderId());

        List<CcHkMain> ccHkMainList = ccHkMainMapper.selectCcHkMainList(searchHkMain);

        if(ccHkMainList!=null&&ccHkMainList.size()>0){
            throw new ServiceException("客户订单已存在回款记录");

        }
        ccHkMain.setCreateTime(DateUtils.getNowDate());
        return ccHkMainMapper.insertCcHkMain(ccHkMain);
    }

    /**
     * 修改回款主表
     * 
     * @param ccHkMain 回款主表
     * @return 结果
     */
    @Override
    public int updateCcHkMain(CcHkMain ccHkMain)
    {
        ccHkMain.setUpdateTime(DateUtils.getNowDate());
        return ccHkMainMapper.updateCcHkMain(ccHkMain);
    }

    /**
     * 批量删除回款主表
     * 
     * @param ids 需要删除的回款主表主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCcHkMainByIds(Long[] ids)
    {
        for (Long id : ids) {
            CcHkInfo ccHkInfo = new CcHkInfo();
            ccHkInfo.setMainId(id);
            List<CcHkInfo> ccHkInfoList = ccHkInfoMapper.selectCcHkInfoList(ccHkInfo);
            if(ccHkInfoList!=null&&ccHkInfoList.size()>0){
                throw new ServiceException("编号："+id+"下有明细，无法删除!");
            }
        }
        return ccHkMainMapper.deleteCcHkMainByIds(ids);
    }

    /**
     * 删除回款主表信息
     * 
     * @param id 回款主表主键
     * @return 结果
     */
    @Override
    public int deleteCcHkMainById(Long id)
    {
        CcHkInfo ccHkInfo = new CcHkInfo();
        ccHkInfo.setMainId(id);
        List<CcHkInfo> ccHkInfoList = ccHkInfoMapper.selectCcHkInfoList(ccHkInfo);
        if(ccHkInfoList!=null&&ccHkInfoList.size()>0){
            throw new ServiceException("编号："+id+"下有明细，无法删除!");
        }
        return ccHkMainMapper.deleteCcHkMainById(id);
    }

    @Override
    public List<CcHkMain> mobileCcHkMainList(CcHkMain ccHkMain) {
       return ccHkMainMapper.mobileCcHkMainList(ccHkMain);
    }
}
